//
//  ZZHelper.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/13.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZHelper.h"
#import <sys/utsname.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
@implementation ZZHelper

+ (NSString *)deviceIPAdress {
    NSString *address = @"an error occurred when obtaining ip address";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    success = getifaddrs(&interfaces);
    if (success == 0) { // 0 表示获取成功
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in  *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    freeifaddrs(interfaces);
    NSLog(@"%@", address);
    return address;
    
    
}
+ (NSString *)ucode {
    NSString *svStr = @"2F0A3C1FA04BB54D9F428C73077FBFB7E8539880A4F4F68DD9E364223174A9492A1C0F45F85B0DE8";
    int value =  666;
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-hh-mm"];
    NSDate *date = [NSDate date];
    NSString *dateStr = [formatter stringFromDate:date];
    
    NSArray *days = [dateStr componentsSeparatedByString:@"-"];
    
    NSString *minutesStr = days[2];
    
    NSInteger minute = [minutesStr integerValue];
    NSInteger minute_first = [[minutesStr substringToIndex:1] integerValue];
    
    NSString *afterSubSvStr = [[svStr substringWithRange:NSMakeRange(minute_first, minute+1-minute_first)] lowercaseString];
    
    NSInteger couters = 0;
    for(int i =0; i < [afterSubSvStr length]; i++){
       NSString *temp = [afterSubSvStr substringWithRange:NSMakeRange(i, 1)];
        if ([temp isEqualToString:@"a"]) {
            couters += 10;
        }else if ([temp isEqualToString:@"b"]){
            couters += 11;
        }else if ([temp isEqualToString:@"c"]){
            couters += 12;
        }else if ([temp isEqualToString:@"d"]){
            couters += 13;
        }else if ([temp isEqualToString:@"e"]){
            couters += 14;
        }else if ([temp isEqualToString:@"f"]){
            couters += 15;
        }else{
            couters += [temp integerValue];
        }
    }

    NSInteger dateInt = [[[dateStr componentsSeparatedByString:@"-"] componentsJoinedByString:@""] integerValue];
    NSString *lStr = [NSString stringWithFormat:@"%ld",dateInt*value + couters];
    return  lStr;
}
+ (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
+ (NSString *)createCUID{
    NSString *result;
    CFUUIDRef uuid;
    CFStringRef uuidStr;
    uuid = CFUUIDCreate(NULL);
    uuidStr = CFUUIDCreateString(NULL, uuid);
    result =[NSString stringWithFormat:@"%@",uuidStr];
    CFRelease(uuidStr);
    CFRelease(uuid);
    return result;
}

@end

@implementation UIView (Image)
- (UIImage*) Getimage
{
    //模糊截图    UIGraphicsBeginImageContext(self.bounds.size);
    //高清截图
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //    CGContextRelease(ctx);
    return tImage;
}

@end
@implementation TTTAttributedLabel (helper)

- (void)setStlyeByText:(NSString *)Text andLinkText:(NSString *)linkText{
    self.font = [UIFont systemFontOfSize:12];
    self.text = Text;
    NSRange Range = [self.text rangeOfString:linkText options:NSCaseInsensitiveSearch];
    [self addLinkToURL:[NSURL URLWithString:@"http://www.assetzz.com"] withRange:Range];
    NSMutableAttributedString *mstr = [[NSMutableAttributedString alloc]initWithString:self.text attributes:@{NSForegroundColorAttributeName:RGBCOLOR(171, 171, 171)}];
    [mstr addAttribute:NSForegroundColorAttributeName value:ZZGoldColor range:Range];
    self.attributedText = mstr;
}

@end

@implementation NSDictionary (helper)

- (NSString*)DataTOjsonString
{
    NSString *jsonString = nil;
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:nil
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
//    NSLog(@"%@",jsonString);
    return jsonString;
}

- (NSString*)DataTOjsonStringAndAddVersion
{
    NSString *jsonString = nil;
    NSError *error;
//    NSDictionary *dict = [self objectForKey:@"parameters"];
    NSMutableDictionary *mdict = self.mutableCopy;
     [mdict setObject:@"100" forKey:@"apiVersion"];
//     phoneSys   sysVersion
//    [mdict setObject:@"iOS" forKey:@"appPlatform"];
//    [mdict setObject:@"" forKey:@"uCode"];
    [mdict setObject:@"中增理财" forKey:@"appPackageName"];
    [mdict setObject:@"11" forKey:@"sysVersion"];
    [mdict setObject:@"iOSIMEI______" forKey:@"phoneId"];
    [mdict setObject:@"" forKey:@"channel"];

    [mdict setObject:@"100" forKey:@"apiVersion"];
    //     phoneSys   sysVersion
    [mdict setObject:@"iOS" forKey:@"phoneSys"];
    [mdict setObject:@"11" forKey:@"sysVersion"];
    [mdict setObject:@"2" forKey:@"appPlatform"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mdict
                                                       options:nil
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    //    NSLog(@"%@",jsonString);
    return jsonString;
}
- (NSString*)DataTOjsonStringAndAddVersionOnlyForLogin
{
    NSString *jsonString = nil;
    NSError *error;

    NSMutableDictionary *mdict = self.mutableCopy;
    [mdict setObject:@"iOS" forKey:@"phoneSys"];
    [mdict setObject:@"11" forKey:@"sysVersion"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mdict
                                                       options:nil
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    //    NSLog(@"%@",jsonString);
    return jsonString;
}

@end
@implementation UIView (ViewControl)

-(UIViewController *)viewControl{
    UIResponder * next = self.nextResponder;
    while (next) {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end

@implementation NSString (Helper)
-(BOOL)JudgeTheillegalCharacter{
    //判断字符串是否含有中文
    for(int i=0; i< [self length];i++){
        
        int a = [self characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        return NO;
        
        }
    return YES;
}

@end
